/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 洪汤泽大帅哥
 * Date: 2024-10-13
 * Time: 15:03
 */
public class TestDemo {
    public static int myBinarySearch(int[] arr, int key) {
        int left = 0;
        int right = arr.length-1;
        while(left<=right) {
            int mid = (left+right)/2;
            if(arr[mid]<key) {
                left=mid+1;
            }
            else if(arr[mid]>key) {
                right = mid - 1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        int ret = myBinarySearch(array,4);
        System.out.println(ret);

    }
}
